'''
Company: TWL
Author: xue jian
Email: xuejian@kanzhun.com
Date: 2020-11-18 17:53:37
'''
#
# @lc app=leetcode.cn id=29 lang=python3
#
# [29] 两数相除
#

# @lc code=start
class Solution:
    def divide(self, dividend: int, divisor: int) -> int:
        def recurse(divid, divis):
            if divid<divis:
                return 0
            if not divid:
                return 0
            ans, tmp=1, divis
            while divis+divis<=divid:
                ans+=ans
                divis+=divis
            return ans+recurse(divid-divis, tmp)

        if divisor==1:
            return dividend
        if divisor==-1:
            return -dividend if dividend>-2147483648 else 2147483647
        if not dividend:
            return dividend
        flag=-1 if (dividend>0 and divisor<0) or (dividend<0 and divisor>0) else 1
        dividend = abs(dividend)
        divisor = abs(divisor)
        return recurse(dividend, divisor) if flag>0 else -recurse(dividend, divisor)
        
# @lc code=end

if __name__ == "__main__":
    dividend=-2147483648
    divisor=-1
    solution=Solution()
    print(solution.divide(dividend, divisor))